// pages/kanjia/join/index.js
var app = getApp(),
  a = app.requirejs("core");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gray_star: "http://ddfwz.sssvip.net/asmce/yigou/gray-star.png",
    light_star: "http://ddfwz.sssvip.net/asmce/yigou/light-star.png",
    store_pic_array: [],
    score: [1, 1, 1, 1, 1],
    total_score: 0,
    url: getApp().globalData.api,
    config: getApp().config
  },
  score: function (e) {
    // console.log(e)
    var that = this,
      index = a.pdata(e).index,
      total_score = (index + 1),
      arr = [1, 1, 1, 1, 1];
    for (var i in arr) {
      if (i <= index) {
        arr[i] = 2
      }
    }
    that.setData({ score: arr, total_score: total_score })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



  /**
   * 提交【评论
   */
  formSubmit: function (e) {
    var that = this;
    var score = that.data.total_score;
    var formData = e.detail.value;
    if (formData.info == '') {
      a.alert('内容不能为空！');
      return;
    }
    if (score == 0) {
      a.alert('您还没有评分哦');
      return;
    }
    formData.fraction = score;
    formData.user_id = getApp().getCache("userinfo").uid;
    formData.app_id = getApp().globalData.appid;
    formData.array_pic = that.data.store_pic_array;
    //console.log(formData);
    a.post('Index/WriteComment',
      formData, function (t) {
        console.log(t)
        if (t.code == 0) {
          //成功操作
          a.alert('提交成功', function () {
            a.jump('', 5);
          })
        } else {
          //失败提示
          a.alert(t.msg)
        }
      })
  },
  chooseImageTap: function () {
    var _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    var _this = this;
    var site = app.siteInfo;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        res.tempFilePaths.forEach(function (t) {
          console.log(t)
          wx.uploadFile({
            url: site.siteroot + '?i=' + site.uniacid + '&t=undefined&v=' + site.version +'&from=wxapp&c=entry&a=wxapp&do=index_uploadFile&path=comment&m='+site.name+'&sign=1d917db727d0aa4e23ca117826fa3153',
            filePath: t,
            name: 'file_upload',
            success: function (res) {
              console.log(res)
              if (res.data != null) {
                var store_pic_array = _this.data.store_pic_array.concat(res.data);
                _this.setData({
                  store_pic_array: store_pic_array
                })
              }
            }
          })
        })

      }
    })
  },
  Image_del: function (e) {
    var that = this,
      index = a.pdata(e).index,
      arr = that.data.store_pic_array;
    a.removeByValue(arr, index, function (i) {
      that.setData({
        store_pic_array: i
      })
    })
  },

})